package com.imufe.chemicalenterprisedangerwarning.security;

import com.imufe.chemicalenterprisedangerwarning.entity.po.UserPO;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;

/**
 * @author BaiBan
 * @since 2022/10/13
 */
public class UserDetail implements UserDetails, Serializable {

    private static final long serialVersionUID = 100L;

    private UserPO user;

    private List<? extends GrantedAuthority> authorities;

    public UserDetail() {

    }

    public UserDetail(UserPO user, List<String> authorityList) {
        this.user = user;
        this.authorities = authorityList
                .stream()
                .map(SimpleGrantedAuthority::new)
                .toList();
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return authorities;
    }

    @Override
    public String getPassword() {
        return user.getPassword();
    }

    @Override
    public String getUsername() {
        return user.getUsername();
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }
}
